import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../controllers/auth_controller.dart';
import '../../../routes/app_routes.dart';

/// 注册控制器
class RegisterController extends GetxController {
  // 注入认证控制器
  final AuthController _authController = Get.find<AuthController>();
  
  // 表单key
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  
  // 输入控制器
  final TextEditingController nameController = TextEditingController();
  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController confirmPasswordController = TextEditingController();
  
  // 加载状态
  final RxBool isLoading = false.obs;
  
  // 密码可见性
  final RxBool isPasswordVisible = false.obs;
  final RxBool isConfirmPasswordVisible = false.obs;
  
  // 同意条款
  final RxBool agreeToTerms = false.obs;
  
  @override
  void onClose() {
    nameController.dispose();
    emailController.dispose();
    passwordController.dispose();
    confirmPasswordController.dispose();
    super.onClose();
  }
  
  /// 执行注册操作
  Future<void> register() async {
    if (!agreeToTerms.value) {
      Get.snackbar(
        '提示',
        '请先阅读并同意服务条款和隐私政策',
        snackPosition: SnackPosition.BOTTOM,
      );
      return;
    }
    
    if (formKey.currentState!.validate()) {
      isLoading.value = true;
      
      try {
        // 调用认证控制器的注册方法
        await _authController.register(
          nameController.text,
          emailController.text,
          passwordController.text,
        );
        
        // 注册成功后，AuthController内部会处理导航
        // 通常是导航到主页
        
      } catch (e) {
        // 显示错误提示
        Get.snackbar(
          '注册失败',
          e.toString(),
          snackPosition: SnackPosition.BOTTOM,
          backgroundColor: Colors.red,
          colorText: Colors.white,
        );
      } finally {
        isLoading.value = false;
      }
    }
  }
  
  /// 切换密码可见性
  void togglePasswordVisibility() {
    isPasswordVisible.value = !isPasswordVisible.value;
  }
  
  /// 切换确认密码可见性
  void toggleConfirmPasswordVisibility() {
    isConfirmPasswordVisible.value = !isConfirmPasswordVisible.value;
  }
  
  /// 切换同意条款状态
  void toggleAgreeToTerms(bool? value) {
    if (value != null) {
      agreeToTerms.value = value;
    }
  }
  
  /// 导航到登录页面
  void navigateToLogin() {
    Get.offNamed(AppRoutes.LOGIN);
  }
  
  /// 打开条款页面
  void openTermsPage() {
    Get.snackbar('提示', '查看服务条款功能正在开发中');
  }
  
  /// 打开隐私政策页面
  void openPrivacyPolicyPage() {
    Get.snackbar('提示', '查看隐私政策功能正在开发中');
  }
  
  // 验证规则
  String? validateName(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入姓名或昵称';
    }
    return null;
  }
  
  String? validateEmail(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入邮箱';
    }
    if (!GetUtils.isEmail(value)) {
      return '请输入有效的邮箱地址';
    }
    return null;
  }
  
  String? validatePassword(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入密码';
    }
    if (value.length < 6) {
      return '密码长度不能少于6位';
    }
    return null;
  }
  
  String? validateConfirmPassword(String? value) {
    if (value == null || value.isEmpty) {
      return '请再次输入密码';
    }
    if (value != passwordController.text) {
      return '两次输入的密码不一致';
    }
    return null;
  }
} 